#URL    = "http://get.qt.nokia.com/qt/source/qt-everywhere-opensource-src-%(VERSION)s.zip"
SRCURL    = "http://get.qt.nokia.com/qt/source/qt-everywhere-opensource-src-%(VERSION)s.tar.gz"
ZIPURL    = "http://get.qt.nokia.com/qt/source/qt-everywhere-opensource-src-%(VERSION)s.zip"
ZIPURL2    = "http://releases.qt-project.org/qt4/source/qt-everywhere-opensource-src-%(VERSION)s.zip"
OLDZIPURL3    = "http://releases.qt-project.org/archive/qt/%(SHORTVERSION)s/%(VERSION)s/qt-everywhere-opensource-src-%(VERSION)s.zip"
ZIPURL3    = "http://download.qt-project.org/archive/qt/%(SHORTVERSION)s/%(VERSION)s/qt-everywhere-opensource-src-%(VERSION)s.zip"

DEFAULT_VERSION = "4.8.4"
VERSIONS = ["4.5.3", "4.6.2", "4.6.3", "4.7.1", "4.7.2", "4.8.0", "4.8.1", "4.8.4"]
NEWURLVERSIONS = ["4.8.4"]

def urldecider( machine, dirs, version):
  linuxurl = SRCURL % {"VERSION" : version}
  winurl =  (ZIPURL, ZIPURL2)[version in NEWURLVERSIONS] % {"VERSION" : version}
  return { "Linux" : linuxurl,
           "Windows" : winurl }[machine.platform]

def newurldecider( machine, dirs, version):
  return ZIPURL3 % {"SHORTVERSION" : version[0:3], "VERSION" : version}

URL = newurldecider
#QT_TARGET   =  "qt-everywhere-opensource-src-4.7.1"

DEPENDS = ["jom"]

import os, shutil
from source.builders import GenericBuilder
from subprocess import Popen, PIPE
from source.machine import Tools

#todo, needed by qt dependend builders like qtpropertybrowser and osg
#def getqmake( machine, directories):
#  """ return the qmake executable full path """
#  if machine.isWindows():
#    return directories.enviro + "/" + QT_TARGET + "/bin/qmake.exe"
#  else:
#    return directories.enviro + "/" + QT_TARGET + "/bin/qmake"

#def getqtdir( machine, directories):
#  """ return the qt installation (full) path """
#  return directories.enviro+"/"+QT_TARGET

class QT4OSBuilder( GenericBuilder ):
  """ subclass of ConfigureMakeBuilder with custom configure step """

  def hasdebug(self):
    # We build debug and release at once.
    return False

  def configureparameters(self):
    params = ['debug-and-release', 'opensource', 'qt-libpng' ,'qt-libjpeg', 'qt-libmng', 
        "fast",
        ]
    if self.machine.isWindows():
      params += ["mp"]
    return params

  def configure(self ):
    """configure qt4, sends yes to the license question """
    os.chdir( self.extractedpath )

    os.environ['QTDIR'] = self.extractedpath
    os.environ['PATH'] =  os.pathsep.join([os.environ['QTDIR']+os.sep+"bin",
                                           self.extractedpath+os.sep+"bin",
                                           os.environ['PATH']])

    # Copy the mkspecs directory as a work around.
    print "Copy mkspecs directory to: ", self.installpath()

    if os.path.exists(self.installpath() + "/mkspecs"):
      shutil.rmtree( self.installpath() + "/mkspecs" )
    self.copydir( "mkspecs", self.installpath() + "/mkspecs" )

    if self.machine.isWindows():
      os.environ['QMAKESPEC'] = ("win32-msvc2005", "win32-msvc2008")[self.machine.isVS2008()]

    self.configureCall(True)

  def configureCall(self, useprefix = False):
    configure = ("./configure", "configure.exe")[self.machine.isWindows()]

    parameters = self.configureparameters()
    parameters += ""
    if parameters != None:
      fixed = ["-" + x for x in parameters]
      cmd = configure + ("", " -prefix " + self.installpath())[useprefix] + " " + " ".join( fixed )
    else:
      cmd = configure + ("", " -prefix " + self.installpath())[useprefix]

    print "calling:", cmd
    p1 = Popen(cmd, shell=True, stdin=PIPE)
    p1.communicate('yes\n')
  
  def make(self):
    os.chdir( self.extractedpath )
    if self.machine.isLinux():
      self.execute( "make -j " + str(self.machine.cores) )
    else:
      self.execute("nmake") #self.dependencies["jom"].jomexecutable()

  def install(self ):
    os.chdir( self.extractedpath )
    #self.configureCall(True)
    if self.machine.isLinux():
      self.execute( "make install" )
    else:
      self.execute( "nmake install" ) #self.dependencies["jom"].jomexecutable()+" install"


BUILDER = QT4OSBuilder

#def buildwindows( machine, dirs ):
  #build_generic.build( machine, dirs, QT4OS_ZIP_URL, QT4OSConfigureMakeBuilder, ['debug-and-release', 'opensource', 'qt-libpng' ,'qt-libjpeg', 'qt-libmng'], copy_binaries = True )

